package com.forezp.exception;

import com.forezp.dto.RespDTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class CommonExceptionHandler {

    @ExceptionHandler(CommonException.class)
    public ResponseEntity<RespDTO> handleException(final Exception e) {
        RespDTO resp = new RespDTO();
        if (e instanceof CommonException) {
            CommonException taiChiException = (CommonException) e;
            resp.setCode(taiChiException.getCode());
            resp.setError(e.getMessage());
        } else {
            resp.setCode(ErrorCode.FAIL.getCode());
            resp.setError(e.getMessage());
        }
        return new ResponseEntity(resp, HttpStatus.OK);
    }
}
